package day07;

import javax.swing.*;
import java.awt.*;

public class Demo01 {
    public static void main(String[] args)  {
        JFrame f = new JFrame("小球运动");
        f.setSize(300,400);
        f.setResizable(false);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BallPanel bp = new BallPanel();
        f.add(bp);
        f.setVisible(true);
        bp.action();
    }
}

class BallPanel extends JPanel {
    int x = 30;
    int y = 30;
    int f=0;//从上往下动是0 从下往上动1
    @Override
    public void paint(Graphics g) {
        g.clearRect(0,0,300,400);
        g.fillOval(x,y,20,20);
    }
    public void action() {
        while(true){
            if(f==0){
                y+=3;
            }
            if(f==1){
                y-=3;
            }
            if(y>=350){
                if(f==0){
                    f=1;
                }
            }
            if(y<=0){
                if(f==1){
                    f=0;
                }
            }

            try {
                Thread.sleep(1000/24);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            repaint();

        }
    }
}
